/*
 * MessageDAO.java
 *
 * Created on June 24, 2007, 4:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jhub.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import jhub.UserSession;
import jhub.text.DCRawCommand;

/**
 *
 * @author dbotelho
 */
public class MessageDAO extends AbstractDAO {
    private final PreparedStatement addMessageStatement;
    
    /** Creates a new instance of MessageDAO */
    public MessageDAO(Connection conn) throws SQLException {
        super(conn);
        this.addMessageStatement = conn.prepareStatement("INSERT INTO \"message\" (id,hub_session_id,user_session_id,raw_message,timestamp,message_type,direction_type) VALUES(?,?,?,?,?,?,?)");
    }
    
    public void addMessage(UserSession userSession,DCRawCommand raw,String direction) throws SQLException{
        
        addMessageStatement.setInt(1,super.nextValue("message_id_seq"));
        addMessageStatement.setInt(2,userSession.getHubSession().getSessionID());
        addMessageStatement.setInt(3,userSession.getSessionID());
        
        addMessageStatement.setString(4,raw.getCommand().toString());
        
        addMessageStatement.setLong(5,raw.getTimeStamp().getTime());    

        addMessageStatement.setString(6,"message");
        addMessageStatement.setString(7,direction);

        System.out.println(addMessageStatement.toString());
        addMessageStatement.executeUpdate();
    }
}
